package edu.gatech.oad.antlab.m7final;
import java.util.Random;
import javax.swing.*;

/**
 * This is the EventController class.
 * This class implements a Random Event.
 * @author Aditya Desai
 * @version 1.0 11/1/11
 */

public class EventController {

	private enum EventEnum {STORM, ILL, WAGON_DAMAGED, INVENTORY_LOSS, PLAYER_DEATH, SETTLER_DEATH, ROBBERY, RECOVERED, WAGON_FLIP, OX_DEAD, OX_TIRED, ADD_FOOD};
	private EventEnum myEvent;
	private int randNum1;
	private int randNum2;
	private int randNum3;
	private int randNum4;
	private int randNum5;
	private int randNum6;
	private Random generator;
	private WagonController wagonController;
	private InventoryController inventoryController;
	private JourneyController journeyController;
	private Game game;


	/**
	 * This is the generateEvent Method
	 * This method generates a random event.
	 * @return String the Random Event
	 */

	public String generateEvent() {
		generator = new Random();
		randNum1 = generator.nextInt(12);
		randNum2 = generator.nextInt(4);
		randNum3 = generator.nextInt(3) + 1;
		randNum4 = generator.nextInt(5);
		randNum5 = generator.nextInt(2);
		randNum6 = generator.nextInt(6);

		switch (randNum1) {

		//There is a storm
		case 0:
			myEvent = EventEnum.STORM;
			for (int i = 0; i < randNum3; i++) {
				journeyController.rest();
			}
			JOptionPane.showMessageDialog(null, "A Storm has hit! You must rest for" + randNum3 + "day(s) until it passes.");
			break;

			//A settler or the player is ill
		case 1:
			myEvent = EventEnum.ILL;
			switch (randNum4) {
			case 0:
				wagonController.setSettlerHealth(wagonController.getSettler1(), "ill");
				JOptionPane.showMessageDialog(null, wagonController.getSettler1().getName()+ "is ill.");
				break;
			case 1:
				wagonController.setSettlerHealth(wagonController.getSettler2(), "ill");
				JOptionPane.showMessageDialog(null, wagonController.getSettler2().getName()+ "is ill.");
				break;
			case 2:
				wagonController.setSettlerHealth(wagonController.getSettler3(), "ill");
				JOptionPane.showMessageDialog(null, wagonController.getSettler3().getName()+ "is ill.");
				break;
			case 3:
				wagonController.setPlayerHealth(wagonController.getPlayer(), "ill");
				JOptionPane.showMessageDialog(null, wagonController.getPlayer().getName()+ "is ill.");
			default:
				wagonController.setSettlerHealth(wagonController.getSettler4(), "ill");
				JOptionPane.showMessageDialog(null, wagonController.getSettler4().getName()+ "is ill.");
				break;
			}
			break;

			//The Wagon is Damaged
		case 2:
			myEvent = EventEnum.WAGON_DAMAGED;
			switch (randNum3) {
			case 1:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "The wagon is damaged! You must use a wheel from inventory to fix it.");
				break;
			case 2:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "The wagon is damaged! You must use an axle from inventory to fix it.");
				break;
			default:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "The wagon is damaged! You must use a tongue from inventory to fix it.");
				break;
			}
			break;

			//The Player Dies
		case 3:
			myEvent = EventEnum.PLAYER_DEATH;
			wagonController.setPlayerHealth(wagonController.getPlayer(), "dead");
			JOptionPane.showMessageDialog(null, "You have died on the trail! The game is over!");
			game.endGame();
			break;

			//A Settler Dies
		case 4:
			myEvent = EventEnum.SETTLER_DEATH;
			switch (randNum2) {
			case 0:
				wagonController.setSettlerHealth(wagonController.getSettler1(), "dead");
				JOptionPane.showMessageDialog(null, wagonController.getSettler1().getName() + "has died on the trail!");
				break;
			case 1:
				wagonController.setSettlerHealth(wagonController.getSettler2(), "dead");
				JOptionPane.showMessageDialog(null, wagonController.getSettler2().getName() + "has died on the trail!");
				break;
			case 2:
				wagonController.setSettlerHealth(wagonController.getSettler3(), "dead");
				JOptionPane.showMessageDialog(null, wagonController.getSettler3().getName() + "has died on the trail!");
				break;
			default:
				wagonController.setSettlerHealth(wagonController.getSettler4(), "dead");
				JOptionPane.showMessageDialog(null, wagonController.getSettler4().getName() + "has died on the trail!");
				break;
			}
			break;

			//Wagon robbed by UGA Student
		case 5:
			myEvent = EventEnum.ROBBERY;
			switch (randNum6) {
			case 0:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole an ox from your wagon!");
				break;
			case 1:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole food from your wagon!");
				break;
			case 2:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(2).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole clothing from your wagon!");
				break;
			case 3:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(3).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole a box of ammo from your wagon!");
				break;
			case 4:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole a wheel from your wagon!");
				break;
			case 5:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole an axle from your wagon!");
				break;
			default:
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
				JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole a tongue from your wagon!");
				break;
			}
			break; 

			//Any ill member has recovered
		case 6:
			myEvent = EventEnum.RECOVERED;
			HealthEnum settler1Health = wagonController.getSettler1().getHealth();
			HealthEnum settler2Health = wagonController.getSettler2().getHealth();
			HealthEnum settler3Health = wagonController.getSettler3().getHealth();
			HealthEnum settler4Health = wagonController.getSettler4().getHealth();
			if (settler1Health.equals(HealthEnum.ILL))
				wagonController.setSettlerHealth(wagonController.getSettler1(), "healthy");
			if (settler2Health.equals(HealthEnum.ILL))
				wagonController.setSettlerHealth(wagonController.getSettler2(), "healthy");
			if (settler3Health.equals(HealthEnum.ILL))
				wagonController.setSettlerHealth(wagonController.getSettler3(), "healthy");
			if (settler4Health.equals(HealthEnum.ILL))
				wagonController.setSettlerHealth(wagonController.getSettler4(), "healthy");
			else
				wagonController.setPlayerHealth(wagonController.getPlayer(), "healthy");
			JOptionPane.showMessageDialog(null, "The ill members of your party have recovered!");
			break;

			//Wagon has tipped over
		case 7:
			myEvent = EventEnum.WAGON_FLIP;
			switch (randNum5) {
			case 0:
				switch (randNum2) {
				case 0:
					wagonController.setSettlerHealth(wagonController.getSettler1(), "dead");
					JOptionPane.showMessageDialog(null, wagonController.getSettler1().getName() + "has drowned!");
					break;
				case 1:
					wagonController.setSettlerHealth(wagonController.getSettler2(), "dead");
					JOptionPane.showMessageDialog(null, wagonController.getSettler2().getName() + "has drowned!");
					break;
				case 2:
					wagonController.setSettlerHealth(wagonController.getSettler3(), "dead");
					JOptionPane.showMessageDialog(null, wagonController.getSettler3().getName() + "has drowned!");
					break;
				default:
					wagonController.setSettlerHealth(wagonController.getSettler4(), "dead");
					JOptionPane.showMessageDialog(null, wagonController.getSettler4().getName() + "has drowned!");
					break;
				}
			default:
				switch (randNum6) {
				case 0:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! An ox was lost from your wagon!");
					break;
				case 1:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! Food was lost from your wagon!");
					break;
				case 2:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(2).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! Clothing was lost from your wagon!");
					break;
				case 3:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(3).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! A box of ammo was lost from your wagon!");
					break;
				case 4:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! A wheel was lost from your wagon!");
					break;
				case 5:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! An axle was lost from your wagon!");
					break;
				default:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! A tongue was lost from your wagon!");
					break;
				}
				break;

				//Ox is dead
			case 8:
				myEvent = EventEnum.OX_DEAD;
				if(inventoryController.getWagonInventory().get(0).getQuantity() == 0){
					game.endGame();
				}
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
				break;

				//Ox is tired
			case 9:
				myEvent = EventEnum.OX_TIRED;
				wagonController.getWagon().setPace(Pace.Steady);
				JOptionPane.showMessageDialog(null, "The oxen are tired! You must move at a steady pace.");
				break;

				//Hunting Party adds Food
			case 10:
				myEvent = EventEnum.ADD_FOOD;
				inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()+20);
				JOptionPane.showMessageDialog(null, "A hunting party has noticed you are short on food. 20 pounds of food has been added to your wagon!");
				break;

				//Random Inventory Loss
			case 11:
				myEvent = EventEnum.INVENTORY_LOSS;
				switch (randNum6) {
				case 0:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "An ox was lost from your wagon!");
					break;
				case 1:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "Food was lost from your wagon!");
					break;
				case 2:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(2).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "Clothing was lost from your wagon!");
					break;
				case 3:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(3).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A box of ammo was lost from your wagon!");
					break;
				case 4:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A wheel was lost from your wagon!");
					break;
				case 5:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "An axle was lost from your wagon!");
					break;
				default:
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A tongue was lost from your wagon!");
					break;
				}
				break;
			}

			return myEvent.toString();
		}
		return null;
	}

}